package tom.client.admin;

import java.util.Map;

import com.google.gwt.core.client.GWT;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.shared.Company;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;

public class FormCompany extends DataForm {
	public FormCompany() {
		lbCode.setText("รหัสบริษัท");

		textName = new TextBox();
		add(textName, 120, 106);
		textName.setSize("250px", "18px");

		Label lb1 = new Label("ชื่อบริษัท");
		add(lb1, 23, 106);

		textAddress = new TextBox();
		add(textAddress, 120, 138);
		textAddress.setSize("300px", "18px");

		Label lb2 = new Label("ที่อยู่");
		add(lb2, 23, 138);

		textTelephone = new TextBox();
		add(textTelephone, 120, 170);
		textTelephone.setSize("159px", "18px");

		Label lb3 = new Label("เบอร์ติดต่อ");
		add(lb3, 23, 170);
		lb3.setSize("80px", "21px");

		intInvoice = new IntegerBox();
		intInvoice.setAlignment(TextAlignment.RIGHT);
		add(intInvoice, 139, 234);
		intInvoice.setSize("100px", "18px");

		intCash = new IntegerBox();
		intCash.setAlignment(TextAlignment.RIGHT);
		add(intCash, 139, 264);
		intCash.setSize("100px", "18px");

		intCredit = new IntegerBox();
		intCredit.setAlignment(TextAlignment.RIGHT);
		add(intCredit, 139, 294);
		intCredit.setSize("100px", "18px");

		intPayment = new IntegerBox();
		intPayment.setAlignment(TextAlignment.RIGHT);
		add(intPayment, 463, 234);
		intPayment.setSize("100px", "18px");

		intPurchase = new IntegerBox();
		intPurchase.setAlignment(TextAlignment.RIGHT);
		add(intPurchase, 463, 264);
		intPurchase.setSize("100px", "18px");

		Label lb4 = new Label("Invoice#");
		add(lb4, 23, 237);
		lb4.setSize("80px", "21px");

		Label lb5 = new Label("Invoice#(Cash)");
		add(lb5, 23, 267);
		lb5.setSize("104px", "21px");

		Label lb6 = new Label("Invoice#(Credit)");
		add(lb6, 23, 297);
		lb6.setSize("104px", "21px");

		Label lb7 = new Label("Payment#");
		add(lb7, 367, 237);
		lb7.setSize("104px", "21px");

		Label lb8 = new Label("Purchase#");
		add(lb8, 367, 267);
		lb8.setSize("104px", "21px");

		intVoucher = new IntegerBox();
		intVoucher.setAlignment(TextAlignment.RIGHT);
		add(intVoucher, 463, 294);
		intVoucher.setSize("100px", "18px");

		Label lb9 = new Label("Voucher#");
		add(lb9, 367, 297);
		lb9.setSize("104px", "21px");
		intGLBooking = new IntegerBox();
		intGLBooking.setAlignment(TextAlignment.RIGHT);
		add(intGLBooking, 463, 324);
		intGLBooking.setSize("100px", "18px");

		Label lb10 = new Label("GL Booking#");
		add(lb10, 367, 327);
		lb10.setSize("104px", "21px");

		intContactNo = new IntegerBox();
		intContactNo.setAlignment(TextAlignment.RIGHT);
		add(intContactNo, 463, 374);
		intContactNo.setSize("100px", "18px");

		Label lblContact = new Label("Contact#");
		add(lblContact, 367, 378);
		this.setFieldsHandler();
	}

	private static FormCompany panel;

	private final TextBox textName;

	private final TextBox textAddress;

	private final TextBox textTelephone;

	private final IntegerBox intInvoice;

	private final IntegerBox intCredit;

	private final IntegerBox intCash;

	private final IntegerBox intPayment;

	private final IntegerBox intPurchase;

	private final IntegerBox intVoucher;

	private final IntegerBox intGLBooking;

	private Company record;

	public static FormCompany getPanel() {
		if (panel == null)
			panel = GWT.create(FormCompany.class);
		return panel;
	}

	@Override
	protected void search() {
		LookupDialog.show("บริษัท", "comCode", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getAdminService().selectCompany(txtCode.getText(), comCallback);
	}

	@Override
	protected void update() {
		getFields();
		Library.getAdminService().updateCompany(record, delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		Library.getAdminService().insertCompany(record, delCallback);

	}

	@Override
	protected void delete() {
		if (Window
				.confirm("ต้องการลบบริษัท " + textName.getText() + " หรือไม่")) {
			Library.getAdminService().deleteCompany(txtCode.getText(),
					delCallback);
		}
	}

	@Override
	protected void clearFields() {
		super.clearFields();
		textName.setText(null);
		textAddress.setText(null);
		textTelephone.setText(null);
		intInvoice.setValue(null);
		intCash.setValue(null);
		intCredit.setValue(null);
		intPayment.setValue(null);
		intPurchase.setValue(null);
		intVoucher.setValue(null);
		intGLBooking.setValue(null);
		intContactNo.setValue(null);
	}

	@Override
	protected void setFields() {
		if (record != null) {
			super.setFields();
			txtCode.setText(record.getComCode());
			textName.setText(record.getName());
			textAddress.setText(record.getAddress());
			textTelephone.setText(record.getTelephone());
			intInvoice.setValue(record.getInvoiceNo());
			intCash.setValue(record.getCashInvoiceNo());
			intCredit.setValue(record.getCreditInvoiceNo());
			intPayment.setValue(record.getPaymentNo());
			intPurchase.setValue(record.getPurchaseNo());
			intVoucher.setValue(record.getVoucherNo());
			intGLBooking.setValue(record.getGlBookingNo());
			intContactNo.setValue(record.getContactNo());
		} else {
			String code = txtCode.getText();
			clearFields();
			super.setMode(3);
			txtCode.setText(code);
		}

	}

	@Override
	protected void getFields() {
		if (record == null)
			record = new Company();
		record.setComCode(txtCode.getText());
		record.setName(textName.getText());
		record.setAddress(textAddress.getText());
		record.setTelephone(textTelephone.getText());
		record.setInvoiceNo(intInvoice.getValue());
		record.setCashInvoiceNo(intCash.getValue());
		record.setCreditInvoiceNo(intCredit.getValue());
		record.setPaymentNo(intPayment.getValue());
		record.setPurchaseNo(intPurchase.getValue());
		record.setVoucherNo(intVoucher.getValue());
		record.setGlBookingNo(intGLBooking.getValue());
		record.setContactNo(intContactNo.getValue());
	}

	private AsyncCallback<Company> comCallback = new AsyncCallback<Company>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(Company result) {
			record = result;
			setFields();
		}
	};
	private IntegerBox intContactNo;

	@Override
	protected String getReportFile() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}
}
